package com.ecnu.codelearn.controller;

import com.ecnu.codelearn.utils.HammingCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 海明码相关接口（高级计算机网络）
 * Created by cao_peisheng@jiuyv.com on 2020/12/16 18:58
 * @author caopeisheng
 */
@Controller
@Api(value = "海明码相关接口", tags = "海明码相关接口")
public class Hamming {

    @GetMapping("/")
    public String hello() {
        return "hamming";
    }

    @GetMapping("/code")
    @ApiOperation(value = "编码", notes = "编码")
    public @ResponseBody
    String code(String src) {
        return new HammingCode().code(src);
    }

    @GetMapping("/decode")
    @ApiOperation(value = "解码", notes = "解码")
    public @ResponseBody
    Map decode(String src) {
        HammingCode hammingCode = new HammingCode();
        String decode = hammingCode.decode(src);

        Map<String, Object> result = new HashMap<>();
        result.put("code", decode);
        result.put("decodeSuccess", hammingCode.isDecodeSuccess());
        result.put("errorSrc", hammingCode.isErrorSrc());
        result.put("errorIndex", hammingCode.getErrorIndex());
        return result;
    }
}
